<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.yaodian100.core.common.utils.ServerValue"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="/taglibs.jsp"%> 
<!-- 验证 -->
	<script type="text/javascript" charset="UTF-8" src="/js/formValidator_min.js" charset="UTF-8"></script>
	<script type="text/javascript" charset="UTF-8" src="/js/formValidator.js" charset="UTF-8"></script>
	<script type="text/javascript" charset="UTF-8" src="/js/formValidatorRegex.js" charset="UTF-8"></script>
	<!-- 数字验证 -->
	<script type="text/javascript" charset="UTF-8" src="/js/jquery.alphanumeric.js"></script>		
	<!-- 月历 -->
	<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick.js"></script>
	<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick-zh-CN.js"></script>
	<link rel="stylesheet" type="text/css" href="/css/smoothness.datepick.css"/>
	<!-- 時間 -->	
	<script type="text/javascript" src="/js/jquery.timePicker.js"></script>
	<link rel="stylesheet" type="text/css" href="/css/timePicker.css"/>		
	<script language="javascript">
	$().ready( function() {
		$('#startDateParam').datepick({minDate: new Date(${minDate}), showOn: 'both',numberOfMonths:2,
	    	buttonImageOnly: true, buttonImage: '<%=ServerValue.getMidContextPath()%>/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
		$('#endDateParam').datepick({minDate: new Date(), showOn: 'both', numberOfMonths:2,
	    	buttonImageOnly: true, beforeShow:customRange, buttonImage: '<%=ServerValue.getMidContextPath()%>/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
		function customRange (){
			var startDt = $('#startDateParam').val(); //起始日
			var endDt = new Date(parseInt(startDt.substring(0,4),10),parseInt(startDt.substring(5,7),10)-1,parseInt(startDt.substring(8,10),10)); //結束日
			return {minDate:endDt}; 
		}
		$("#timestart").timePicker({step:60, startTime:"00:00", endTime:"23:00"});
		$("#timeend").timePicker({step:60, startTime:"00:59", endTime:"23:59"});
		$("#shelfPriceParam").numeric({allow:"."});
		if ( "" != "${shelf.shelfPrice}" )
			$("#rate").text(round2places(("${shelf.shelfPrice}" - "${product.productCost}")/"${shelf.shelfPrice}"*100)+"%");

		if ( $("#shelfPriceParam").val() != "" )
			$("#rate").text(round2places(($("#shelfPriceParam").val() - "${product.productCost}")/$("#shelfPriceParam").val()*100)+"%");
		
		$("#shelfPriceParam").blur(function(){
			$("#rate").text(round2places(($("#shelfPriceParam").val() - $("#productCost").val())/$("#shelfPriceParam").val()*100)+"%"); 
			//查詢成本资讯-毛利率
			$("td[id*='efDt_']").each(function(index){
					$("#tdRate_"+index).text(round2places(($("#shelfPriceParam").val() - $("#unPrc_"+index).text())/$("#shelfPriceParam").val()*100)+"%"); 
				});
		});

		//查詢成本
		 $("#queryCost").click(function(){
			 if($("#startDateParam").val()=='' || $("#endDateParam").val()==''){
				 alert("请选择日期");
			 }else{
				 $.post("<%=ServerValue.getMidContextPath()%>/shelf/getJsonCheckCost.do",{productId:$("#productId").val(),startDateParam:$("#startDateParam").val(),timestart:$("#timestart").val(),endDateParam:$("#endDateParam").val(),timeend:$("#timeend").val()},costCallback,"json");
				
			 }
		 });
		 //申请
		 $("#submitBtn").click(function(){
			 if ( validate_data()){
					if ( parseFloat($("#shelfPriceParam").val()) < parseFloat($("#cost").val())){
						if(confirm("上架售价小于商品成本价,确定要储存吗?")){
							toSave();
						}
					 	//promptDialog('','上架售价小于商品成本价,确定要储存吗?', 'toSave()');
					}else{
						$("#shelfStatus").val("SHF500");
						$("#shelfForm").submit();
					}
			 }
		 });
		 //下架(停用)
		 $("#cancelBtn").click(function(){
			 if ( validate_data()){
				 $("#shelfStatus").val("SHF700");
				 $("#shelfForm").submit();
			 }
		 });		 
		 $("#notSaveBtn").click(function(){
			 location.href="<%=ServerValue.getMidContextPath()%>/shelf/list.do?productId=${productId}";
		 });
			$("#approveCancelBtn").click(function(){
				if ( "" == $("#approveFailReason").val() ){
					alert("请输入审核失败原因");
					return false;
				}
				$("#shelfForm").attr("action","saveApprove.do");
				$("#shelfStatus").val("SHF600");
				$("#shelfForm").submit();
			});
			
			$("#approveOKBtn").click(function(){
				if ( parseFloat($("#rate").text().replace("%","")) < 0.0){
					if(confirm("上架售价小于商品成本价,确定要储存吗?")){
						toSaveApprove();
					}
				}else{
					toSaveApprove();
				}
			});			 
	});
	function toSave(){
		$("#shelfStatus").val("SHF000");
		$("#shelfForm").submit();
	}
	function toSaveApprove(){
		$("#shelfStatus").val("SHF500");
		$("#shelfForm").attr("action","saveApprove.do");
		$("#shelfForm").submit();
	}
	
	function validate_data(){
		if ( $("#promoteNameParam").val() != "" ){
	    if(parseInt(GetCharLength($("#promoteNameParam").val()),10)>40){
	    	alert("商品促销小标字数限制:20个中文字,40个英文字");
				return false;
	    }		
		}
		if ($("#startDateParam").val() == "" ){
			alert("请输入上架起始日期");
			return false;
		}
		if ($("#timestart").val() == "" ){
			alert("请输入上架起始時間");
			return false;
		}
		if ($("#endDateParam").val() == "" ){
			alert("请输入上架結束日期");
			return false;
		}
		if ($("#timeend").val() == "" ){
			alert("请输入上架結束時間");
			return false;
		}
		if ( !dateDiff($("#startDateParam").val() , $("#timestart").val() ,$("#endDateParam").val()  , $("#timeend").val())){
		 alert("上架結束日期不可大于上架起始日期");
		 return false;
		}
		if ($("#shelfPriceParam").val() == "" ){
			alert("请输入上架售價");
			return false;
		}
		
		<c-rt:if test="${not empty shelf.shelfId}">
			if ( ($("#startDateParam").val() != $("#checkStartDate").val()) && $("#checkStartDate").val()!='' ){
				alert("上架起始日期不可更改");
				return false;
			}
			if ( ($("#timestart").val() != $("#chckTimestart").val()) && $("#chckTimestart").val()!='' ){
				alert("上架起始时间不可更改");
				return false;
			}
		</c-rt:if>
		return true;
	}

	//日期判断,计算天数差的函数
	function dateDiff(sDate1,sTime1, sDate2,sTime2){ //sDate1和sDate2是2002-12-18格式
		 var startDate = new Date(sDate1.split("-")[0],eval(sDate1.split("-")[1])-1,sDate1.split("-")[2],sTime1.split(":")[0],sTime1.split(":")[1],00); 
		 var endDate = new Date(sDate2.split("-")[0],eval(sDate2.split("-")[1])-1,sDate2.split("-")[2],sTime2.split(":")[0],sTime2.split(":")[1],00);
     if ( startDate >= endDate )return false;
     else return true;
	}
	
	//取到小数点第二位
	function round2places(numToRound){ 
		var result; 
		result=numToRound*100; 
		result=Math.round(result); 
		result=result/100; 
		return result;
	} 
	function dateExistCallback(jsonShelfDateExist){
		$("#dateParam").val(jsonShelfDateExist);
	}	
  //查询后台成本
  function costCallback(quotations){
		if(null!=quotations){
			$('#divCost').html("");
			for(ii=0; ii<quotations.length;ii++){
				$('#divCost').append("<tr><td id=efDt_"+ii+">"+quotations[ii].effectiveDateString+"</td><td id=unPrc_"+ii+">"+quotations[ii].unitPrice+"</td><td id=tdRate_"+ii+">"+"</td></tr>");
			}
		}else{
			alert("无成本资讯");
			$('#divCost').html("");
		}
	}
	function GetCharLength(str)
	{
		var iLength = 0;
		for(var i = 0;i<str.length;i++)
			{
			if(str.charCodeAt(i) >255)
			{
				iLength += 2;
			}
			else
			{
				iLength += 1;
			}
		}
		return iLength;
	}
	function CutStr(Str,Len)
	{
		var CurStr = "";
		for(var i = 0;i<Str.length;i++)
			{
				CurStr += Str.charAt(i);
			if(GetCharLength(CurStr )>Len)
			{
				return Str.substring(0,i);
			}
		}
		return CurStr ;
	} 	
	</script>	
<h1>商品管理  - 商品上下架<br/>
<div class="f-link">
	<yaodian100:checkPermission permissions="ROLE_PM_MANAGER,MANAGER_A-20-200-100">
		<a href="<%=ServerValue.getMidContextPath()%>/shelf/searchApprove.do">回商品上架檔审核列表页</a>
	</yaodian100:checkPermission>
	<a href="<%=ServerValue.getMidContextPath()%>/shelf/list.do?productId=${productId}">回上下架列表页</a>
	<a href="<%=ServerValue.getMidContextPath()%>/product/list.do">回商品列表页</a>
</div>
</h1>
<s:actionerror />
<s:actionmessage />
<s:fielderror />
<c-rt:if test="${not empty productId}">
	<jsp:include page="commonMenu.jsp"/>
</c-rt:if>
<s:form action="save.do" method="post" id="shelfForm">
	<table class="tb-data" width="100%">	
	 	<tr>
			<th>商品名称（商品编号)</th>			
			<td>${productName}(${productId})</td>
		</tr>
		<c-rt:choose>
	  	<c-rt:when test="${approve eq 'Y'}">
				<tr>
					<th>商品促销小标</th>			
					<td>${shelf.promoteName}</td>
				</tr>	  	
			  <tr>
					<th>*上架起始日期时间</th>			
					<td><fmt:formatDate value="${shelf.startDate}" pattern="yyyy-MM-dd HH:mm" /></td>
				</tr>
			  <tr>
					<th>*上架结束日期时间</th>			
					<td><fmt:formatDate value="${shelf.endDate}" pattern="yyyy-MM-dd HH:mm" /></td>
				</tr>
			  <tr>
					<th>*上架售价</th>			
					<td>        
					 <fmt-rt:formatNumber pattern="######0.0" value="${shelf.shelfPrice}"/>
				   <span class="txt-rd">【※上架售价小数点1位数后无条件舍去】</span><br/>
				   <table  class="tb-data" width="100%">
				   	<tr>
				   		<th>厂商建议售价</th>
				   		<th>提报耀点价</th>
				   		<th>商品成本</th>
				   		<th>毛利率</th>
				   	</tr>
				   	<tr>
				   		<td style="text-align: center;"><fmt-rt:formatNumber pattern="######0.0" value="${product.listPrice}"/></td>
				   		<td style="text-align: center;"><fmt-rt:formatNumber pattern="######0.0" value="${product.prpsShelfPrice}"/></td>
				   		<td style="text-align: center;"><fmt-rt:formatNumber pattern="######0.00" value="${product.productCost}"/></td>
				   		<td style="text-align: center;"><span id="rate"></span></td>
				   	</tr>
				   </table>
			    </td>
				</tr>
		  	<tr>
					<th>上架类别</th>			
					<td>${shelfTypeMenu.options[shelf.shelfType].name}</td>
			  </tr>					  	
		  	<tr>
					<th>上架状态</th>			
					<td>${shelfStatusMenu.options[shelf.shelfStatus].name}</td>
			  </tr>
			  <tr>
					<th>备注</th>			
					<td>${shelf.shelfMemo}</td>
				</tr>	
				 <tr>
			<th >	建立者<br/>
				建立日期<br/> 
					修改者<br/>
				修改日期
				</th><td>
					${shelf.createUser}<br/>
					<fmt-rt:formatDate value="${shelf.createDate}" pattern="yyyy-MM-dd HH:mm:ss" /><br/>
					${shelf.modifyUser}<br/>
				<fmt-rt:formatDate value="${shelf.modifyDate}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
	  	
	  </tr>			
	  	</c-rt:when>
	  	<c-rt:otherwise>
				<tr>
					<th>商品促销小标</th>			
					<td>        
				    <input type="text" name="promoteNameParam" id="promoteNameParam" size="45" maxlength="45" value="${shelf.promoteName}"/>
				    <span class="txt-rd">【※限20个中文字】</span>	       
			    </td>
				</tr>
			  <tr>
					<th>*上架起始日期时间</th>			
					<td>       
						<input type="text" name="startDateParam" id="startDateParam" size="20" value="<fmt:formatDate value="${shelf.startDate}" pattern="yyyy-MM-dd" />"/>	 
			     		<input id="timestart" name="timestart" type="text" value="<fmt:formatDate value="${shelf.startDate}" pattern="HH:mm" />"  size="10"/>
			   	      <input type="hidden" id="checkStartDate" size="20" value="<fmt:formatDate value="${shelf.startDate}" pattern="yyyy-MM-dd" />"/>	
			      <input type="hidden" id="chckTimestart"  value="<fmt:formatDate value="${shelf.startDate}" pattern="HH:mm" />"  size="10"/>       
			    </td>
				</tr>
			  <tr>
					<th>*上架结束日期时间</th>			
					<td>        
				    <input type="text" name="endDateParam" id="endDateParam" size="20" value="<fmt:formatDate value="${shelf.endDate}" pattern="yyyy-MM-dd" />"/>	
				    <input id="timeend" name="timeend" type="text" value="<fmt:formatDate value="${shelf.endDate}" pattern="HH:mm" />" size="10"/>
				   	 <input type="button" id="queryCost" value="查询成本"/>	
				  </td>
				</tr>
			  <tr>
					<th>*上架售价</th>			
					<td>        
					 <fmt-rt:formatNumber pattern="######0.0" value="${shelf.shelfPrice}" var="shelfPrice"/>
				   <input type="text" name="shelfPriceParam" id="shelfPriceParam" size="10" value="${shelfPrice}"/>
				   <c-rt:set var="cost" value="${product.productCost}"/>
				   <input type="hidden" name="cost" id="cost" value="${cost}"/>		   
				   <span class="txt-rd">【※上架售价小数点1位数后无条件舍去】</span><br/><br/>
				   <table  class="tb-data" width="100%">
				   	<tr>
				   		<th>厂商建议售价</th>
				   		<th>提报耀点价</th>
				   		<th>商品成本</th>
				   		<th>毛利率</th>
				   	</tr>
				   	<tr>
				   		<td style="text-align: center;"><fmt-rt:formatNumber pattern="######0.0" value="${product.listPrice}"/></td>
				   		<td style="text-align: center;"><fmt-rt:formatNumber pattern="######0.0" value="${product.prpsShelfPrice}"/></td>
				   		<td style="text-align: center;"><fmt-rt:formatNumber pattern="######0.00" value="${cost}"/></td>
				   		<td style="text-align: center;"><span id="rate"></span></td>
				   	</tr>
				   </table>
			    </td>
				</tr>
				<tr>
					<th>成本资讯</th>			
					<td> 
						<table>
						 <tr>
							<th>生效日</th><th>成本</th><th>毛利率</th>
						 </tr>	
						 <tbody id="divCost"> </tbody>
				  		</table>
			    	</td>
				</tr>
		  	<tr>
					<th>上架类别</th>			
					<td><s:select name="shelfType" list="shelfTypeMenu.options" listKey="key"  listValue="value.name"/></td>
			  </tr>				
		  	<tr>
					<th>上架状态</th>			
					<td>${not empty shelf.shelfStatus ? shelfStatusMenu.options[shelf.shelfStatus].name : shelfStatusMenu.options["SHF000"].name}</td>
			  </tr>
			  <tr>
					<th>备注</th>			
					<td>        
				    <textarea name="shelfMemoParam" cols="80" rows="5">${shelf.shelfMemo}</textarea>       
			    </td>
				</tr>	
				 <tr>
			<th >	建立者<br/>
				建立日期<br/> 
					修改者<br/>
				修改日期
				</th><td>
					${shelf.createUser}<br/>
					<fmt-rt:formatDate value="${shelf.createDate}" pattern="yyyy-MM-dd HH:mm:ss" /><br/>
					${shelf.modifyUser}<br/>
				<fmt-rt:formatDate value="${shelf.modifyDate}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
	  	
	  </tr>			
				</c-rt:otherwise>
		</c-rt:choose>
		<c-rt:if test="${approve eq 'Y'}">
		  <tr>
		  	<th>审核人员</th>
		  	<td>${loginUser}</td>
		  </tr>
		  <tr>
		  	<th>不同意原因</th>
		  	<td><input type="text" name="approveFailReason" id="approveFailReason" maxlength="255" size="100" value="${productSupplierApprove.approveFailReason}"/></td>
		  </tr>	  	  
	  </c-rt:if>		
		<tr>
			<td colspan="2">
				<input type="hidden" id="shelfId" name="shelfId" value="${shelf.shelfId}"/>
				<input type="hidden" id="productId" name="productId" value="${productId}"/>
				<input type="hidden" id="dateParam" name="dateParam" value="" />
				<input type="hidden" id="productCost" name="productCost" value="${product.productCost}" />
				<input type="hidden" name="shelfStatus" id="shelfStatus" value="${shelf.shelfStatus}"/>
				<c-rt:choose>
		  		<c-rt:when test="${approve eq 'Y'}">
						<yaodian100:checkPermission permissions="ROLE_PM_MANAGER">
				  		<input type="button" id="approveCancelBtn" name="approveCancelBtn" value="不同意申请"/>
				  		<input type="button" id="approveOKBtn" name="approveOKBtn" value="同意申请"/>
			  		</yaodian100:checkPermission>	  			  		
		  		</c-rt:when>
		  		<c-rt:otherwise>
						<input type="button" id="submitBtn" value="储存"/>
						<c-rt:if test="${not empty shelf.shelfId}">
							<input type="button" id="cancelBtn" value="下架"/>
						</c-rt:if>
						<input type="button" id="notSaveBtn" name="notSaveBtn" value="放弃修改"/>
					</c-rt:otherwise>
				</c-rt:choose> 
			</td>
		</tr>
	</table>
</s:form>
<br/>
<p>售价记录</p>
<table class="tb-1" width="100%">
	<thead>
		<tr>
			<th>上架类别</th>
			<th>日期起</th>
			<th>日期迄</th>
			<th>小标</th>
			<th>售价</th>
			<th>申请人</th>
			<th>申请状态</th>
			<th>申请时间</th>
			<th>异动人</th>
			<th>异动时间</th>
		</tr>
	</thead>
	<tbody>
		<c-rt:forEach items="${shelfHistoryList}" var="shelf" >
			<tr>
				<td>${shelfTypeMenu.options[shelf['SHELF_TYPE']].name}</td>
				<td><fmt-rt:formatDate value="${shelf['START_DATE']}" pattern="yyyy-MM-dd HH:mm"/></td>
				<td><fmt-rt:formatDate value="${shelf['END_DATE']}" pattern="yyyy-MM-dd HH:mm"/></td>
				<td>${shelf['PROMOTE_NAME']}</td>
				<td><fmt-rt:formatNumber value="${shelf['SHELF_PRICE']}" pattern="######0.0"/></td>
				<td>${shelf['CREATE_USER']}</td>
				<td>${shelfStatusMenu.options[shelf['SHELF_STATUS']].name}</td>
				<td><fmt-rt:formatDate value="${shelf['CREATE_DATE']}" pattern="yyyy-MM-dd HH:mm"/></td>
				<td>${shelf['MODIFY_USER']}</td>
				<td><fmt-rt:formatDate value="${shelf['MODIFY_DATE']}" pattern="yyyy-MM-dd HH:mm"/></td>
			</tr>
		</c-rt:forEach>
	</tbody>
</table>